const { BaseCommand } = require("./base");
const { shareData } = require("./share_data");
let logger;

class LogCommand extends BaseCommand {
    async execute() {
        if (!logger) {
            const log4js = require("log4js");
            log4js.configure({
                appenders: {
                    automator: {
                        type: "file",
                        filename: `${shareData.AUTOMATOR_ROOT}/log/automator.log`,
                    },
                    console: {
                        type: "console",
                    },
                },
                categories: {
                    default: {
                        appenders: ["automator"],
                        level: "debug",
                    },
                },
            });
            logger = log4js.getLogger();
        }

        switch (this.selfData?.level) {
            case "trace": {
                logger.trace(this.content);
                break;
            }
            case "debug": {
                logger.debug(this.content);
                break;
            }
            case "info": {
                logger.info(this.content);
                break;
            }
            case "warn": {
                logger.warn(this.content);
                break;
            }
            case "error": {
                logger.error(this.content);
                break;
            }
            case "fatal": {
                logger.fatal(this.content);
                break;
            }
            case "mark": {
                logger.mark(this.content);
                break;
            }
            default: {
                logger.info(this.content);
                break;
            }
        }
    }

    getRequireContent() {
        return true;
    }
}

module.exports = {
    LogCommand,
};
